package cn.com.yiqi.wsc.service.advertisement.impl;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.dao.advertisement.AdvertisementDao;
import cn.com.yiqi.wsc.dao.advertisement.criteria.AdvertisementCriteria;
import cn.com.yiqi.wsc.dao.advertisement.entity.AdvertisementEntity;
import cn.com.yiqi.wsc.dao.balance.entity.BalanceFetchEntityDTO;
import cn.com.yiqi.wsc.service.advertisement.AdvertisementService;
import cn.com.yiqi.wsc.service.advertisement.PortalAdvertisementService;
import cn.com.yiqi.wsc.service.user.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.SystemEnvironmentPropertySource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 杨贵森
 * @ClassName:PortalAdvertisementServiceImpl
 * @Title: PortalAdvertisementServiceImpl
 * @Description: TODO
 * @date 2017/3/22
 */
@Service
public class PortalAdvertisementServiceImpl implements PortalAdvertisementService {
    @Autowired
    AdvertisementDao advertisementDao;
    @Autowired
    UserService userService;
    @Autowired
    AdvertisementService advertisementService;

    @Override
    public Result searchAdvertisementList(AdvertisementCriteria advertisement){
        Result result = new Result();
        PageHelper.startPage(advertisement.getCurrentPage(), advertisement.getPageSize());
        PageInfo<AdvertisementEntity> advertisementEntityList = new PageInfo<>(advertisementDao.selectAdsList(advertisement));
        result.putData("total",advertisementEntityList.getTotal());
        result.putData("advertisementList",advertisementEntityList.getList());
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        return result;
    }

    @Override
    @Transactional
    public Result upAdvertisement(AdvertisementCriteria advertisementEntity) {
        AdvertisementEntity advertisementEntity1 = advertisementDao.selectUpOne(advertisementEntity.getSort(),advertisementEntity.getType());
        int i=0;
        int j=0;
        if(advertisementEntity1!=null){
            AdvertisementEntity advertisementEntity2  =advertisementDao.selectAdsById(advertisementEntity.getId());
            Integer sort1 = advertisementEntity2.getSort();
            Integer sort2 =advertisementEntity1.getSort();
            advertisementEntity2.setSort(sort2);
            advertisementEntity1.setSort(sort1);
             i = advertisementDao.update(advertisementEntity1);
             j = advertisementDao.update(advertisementEntity2);
        }
        Result result = new Result();
        if(i>0 && j>0 ){
            result.putData("upAdsStatus",1);
        }else{
            result.putData("upAdsStatus",0);
        }
        result.setStatus(ResultStatusEnum.操作成功.getCode());
        return result;
    }


}
